package com.luoye.lj.wstool.wsdm;

import com.luoye.lj.wstool.common.StringUtils;
import com.luoye.lj.wstool.generator.ICodeGenerator;
import com.luoye.lj.wstool.wsdm.entity.WsdtEntity;
import com.luoye.lj.wstool.wsdm.entity.WsdtField;
import com.luoye.lj.wstool.wsdm.entity.WsdtFieldDesc;

/*
 * Created with IntelliJ IDEA.
 * User: Dave Liu
 * Date: 2016/7/7
 * Time: 15:45
 * To change this template use File | Settings | File Templates.
 */
public class HtmlFieldEntityMapCodeGenerator implements ICodeGenerator {
    private WsdtEntity wsdtEntity;

    public HtmlFieldEntityMapCodeGenerator(WsdtEntity wsdtEntity) {
        this.wsdtEntity = wsdtEntity;
    }

    @Override
    public String codeGenerate() {
        String code="";
        if(this.wsdtEntity!=null){
            if(this.wsdtEntity.getFieldList()!=null){
                for(WsdtField wsdtField:this.wsdtEntity.getFieldList()){
                    if(wsdtField.getWsdtFieldDesc()!=null&&wsdtField.getWsdtFieldDesc().getRelatedEntity()!=null){
                        String entityName=wsdtField.getWsdtFieldDesc().getRelatedEntity().getEntityName();
                        String en= StringUtils.makeFirstCharLowercase(entityName);
                        code+="        var "+wsdtField.getFieldName()+entityName+"Map={};\n" +
                                "        \\$(\"#"+wsdtField.getFieldName()+"_edit option\").each(function () {\n" +
                                "            "+wsdtField.getFieldName()+entityName+"Map[\\$(this).val()]=\\$(this).text()\n" +
                                "        });\n";
                    }
                }
            }
        }
        return code;
    }
}
